﻿using FileSC.Configs;
using FileSC.Configs.StatusBar;
using FileSC.DB;
using FileSC.DB.Database;
using FileSC.WEBService.Db;
using FileSC.WEBService.Dto;
using FileSC.Win.api;
using Hangfire;
using Hangfire.Server;
using System;
using System.IO;
using System.Threading.Tasks;

namespace FileSC.WEBService.Job
{
    public class StorageJob
    {
        [Queue("download")]
        public static async Task _DownloadFile(DownloadFileInput input)
        {
            StatusBar.SetIcon(IconType.Download);
            try
            {
                var is_recover = false;
                using (var db = new DownloadJobDB())
                {
                    var info = db.FindByInfoId(input.Id);
                    if (info == null)
                    {
                        db.Update(new DB.Entities.DownloadJobInfo
                        {
                            JobID = input.Id,
                            CreateTime = DateTime.Now,
                            UpdatedTime = DateTime.Now,
                            DownloadSize = 0,
                            FileSize = 0,
                            Path = input.Path,
                            Name = Path.GetFileName(input.Path),
                            Message = "正在加载文件信息",
                            FromOpenFile = false
                        });
                    }
                    else if (info.RunAtNow)
                    {
                        // 重复执行
                        return;
                    }
                    else
                    {
                        //恢复执行
                        db.Update(info);
                        is_recover = true;
                    }

                }

                var path = await Client.MyHttp.DownloadFile(input, is_recover);
                Win32Api.ReleaseWorkingSize();
                if (AppConfig.YesOrNo("下载后打开文件夹"))
                {
                    Client.MyHttp.OpenFolder(path);
                }
            }
            catch
            {
                Win32Api.ReleaseWorkingSize();
                StatusBar.SetIcon(IconType.Error);
                throw;
            }
            StatusBar.SetIcon(IconType.OK);
        }
        [Queue("open")]
        public static async Task _OpenFile(DownloadFileInput input)
        {
            StatusBar.SetIcon(IconType.Download);
            try
            {
                var is_recover = false;
                using (var db = new OpenJobDB())
                {
                    var info = db.FindByInfoId(input.Id);
                    if (info == null)
                    {
                        db.Update(new DB.Entities.OpenJobInfo
                        {
                            JobID = input.Id,
                            CreateTime = DateTime.Now,
                            UpdatedTime = DateTime.Now,
                            Message = "正在下载文件",
                            Name = Path.GetFileName(input.Path),
                            Path = input.Path,
                        });

                        using (var db2 = new DownloadJobDB())
                        {
                            db2.Update(new DB.Entities.DownloadJobInfo
                            {
                                JobID = input.Id,
                                CreateTime = DateTime.Now,
                                UpdatedTime = DateTime.Now,
                                DownloadSize = 0,
                                FileSize = 0,
                                Path = input.Path,
                                Name = Path.GetFileName(input.Path),
                                Message = "正在加载文件信息",
                                FromOpenFile = true
                            });
                        }
                    }
                    else if (info.RunAtNow)
                    {
                        return;
                    }
                    else
                    {
                        //恢复执行
                        db.Update(info);
                        is_recover = true;
                    }
                }

                await Client.MyHttp.OpenFile(input, is_recover);
            }
            catch
            {
                StatusBar.SetIcon(IconType.Error);
                throw;
            }
            StatusBar.SetIcon(IconType.OK);
        }
        [Queue("upload")]
        public static async Task _UploadFile(UploadFileInput input)
        {
            StatusBar.SetIcon(IconType.Download);
            try
            {
                var is_recover = false;
                using (var db = new UploadJobDB())
                {
                    var info = db.FindByInfoId(input.Id);
                    if (info == null)
                    {
                        db.Update(new DB.Entities.UploadJobInfo
                        {
                            JobID = input.Id,
                            CreateTime = DateTime.Now,
                            UpdatedTime = DateTime.Now,
                            UploadSize = 0,
                            FileSize = 0,
                            Path = input.RemotePath,
                            Name = Path.GetFileName(input.RemotePath),
                            LocalPath = input.Path,
                            Message = "正在加载文件信息"
                        });
                    }
                    else if (DBHost.EqualMy(info))
                    {
                        // 重复执行
                        return;
                    }
                    else
                    {
                        //恢复执行
                        db.Update(info);
                        is_recover = true;
                    }

                }

                await Client.MyHttp.UploadFile(input, is_recover);
                Win32Api.ReleaseWorkingSize();
            }
            catch
            {
                StatusBar.SetIcon(IconType.Error);
                throw;
            }
            StatusBar.SetIcon(IconType.OK);
        }
        [Queue("upload-path")]
        public static async Task _UploadPath(UploadPathInput input)
        {
            StatusBar.SetIcon(IconType.Download);
            try
            {
                await Client.MyHttp.UploadPath(input);
                Win32Api.ReleaseWorkingSize();
            }
            catch
            {
                Win32Api.ReleaseWorkingSize();
                StatusBar.SetIcon(IconType.Error);
                throw;
            }
            StatusBar.SetIcon(IconType.OK);
        }
        public static void DownloadFile(DownloadFileInput input)
        {
            BackgroundJob.Enqueue(() =>_DownloadFile(input));
        }

        public static void OpenFile(DownloadFileInput input)
        {
            BackgroundJob.Enqueue(() => _OpenFile(input));
        }
     
        public static void UploadFile(UploadFileInput input)
        {
            BackgroundJob.Enqueue(() => _UploadFile(input));
        }

        public static void UploadPath(UploadPathInput input)
        {
            BackgroundJob.Enqueue(() => _UploadPath(input));
        }
    }
}
